<template>
  <u-navbar :background="background" :is-back="false">
    <view class="slot-wrap">
      <u-image shape="circle" width="50" height="50" :src="userInfo.storeLogo"></u-image>
    </view>
    <!-- #ifndef H5 -->
    <view slot="right" class="slot-right">
      <u-icon size="40" @click="scan()" name="scan"></u-icon>
    </view>
    <!-- #endif -->
  </u-navbar>
</template>

<script>
import config from "@/config/config";
import permision from "@/js_sdk/wa-permission/permission.js"; //权限工具类
import storage from "@/utils/storage.js"; //缓存
export default {
  data() {
    return {
      background: {
        background: "#f6f6f6",
      },
      userInfo: {},
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (storage.getUserInfo().id) {
        this.userInfo = storage.getUserInfo(); //从缓存中获取店铺信息
        console.log("this.userInfo",this.userInfo);
      } else {
        uni.reLaunch({
        	url:"/pages/passport/login"
        })
      }
    },
    seacnCode() {
      uni.scanCode({
        success: function (res) {
          let path = encodeURIComponent(res.result);
          config.scanAuthNavigation.forEach((src) => {
            if (res.result.indexOf(src) != -1) {
              uni.navigateTo({
                url: `/${res.result.substring(src.length)}`,
              });
            } else {
              setTimeout(() => {
                uni.navigateTo({
                  url: "/pages/tabbar/home/web-view?src=" + path,
                });
              }, 100);
            }
          });
        },
      });
    },
    /**
     * 提示获取权限
     */
    tipsGetSettings() {
      uni.showModal({
        title: "提示",
        content: "您已经关闭相机权限,去设置",
        success: function (res) {
          if (res.confirm) {
            if (this.isIos) {
              plus.runtime.openURL("app-settings:");
            } else {
              permision.gotoAppPermissionSetting();
            }
          }
        },
      });
    },
    /**
     * 唤醒客户端扫码
     * 没权限去申请权限，有权限获取扫码功能
     */
    async scan() {
      // #ifdef APP-PLUS
      this.isIos = plus.os.name == "iOS";
      // 判断是否是Ios
      if (this.isIos) {
        const iosFirstCamera = uni.getStorageSync("iosFirstCamera"); //是不是第一次开启相机
        if (iosFirstCamera !== "false") {
          uni.setStorageSync("iosFirstCamera", "false"); //设为false就代表不是第一次开启相机
          this.seacnCode();
        } else {
          if (permision.judgeIosPermission("camera")) {
            this.seacnCode();
          } else {
            // 没有权限提醒是否去申请权限
            this.tipsGetSettings();
          }
        }
      } else {
        /**
         * TODO 安卓 权限已经授权了，调用api总是显示用户已永久拒绝申请。人傻了
         * TODO 如果xdm有更好的办法请在 https://gitee.com/beijing_hongye_huicheng/lilishop/issues 提下谢谢
         */
        this.seacnCode();
      }

      // #endif

      // #ifdef MP-WEIXIN
      this.seacnCode();
      // #endif
    },
  },
};
</script>

<style lang="scss" scoped>
.slot-right {
  margin-right: 32rpx;
}
.slot-wrap {
  display: flex;
  align-items: center;
  /* 如果您想让slot内容占满整个导航栏的宽度 */
  /* flex: 1; */
  /* 如果您想让slot内容与导航栏左右有空隙 */
  padding: 0 32rpx;
}
</style>